﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AutoMapper;
using BaBaoFan.Modifier.Data;
using BaBaoFan.Modifier.Model;
using BaBaoFan.Modifier.Models;
using BaBaoFan.Modifier.Service;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.Extensions.Caching.Memory;

namespace BaBaoFan.Modifier.Controllers
{
    public class NavigationController : BaseController
    {
        private readonly INavigationService navigationService;
        private readonly INavigationRelationService navigationRelationService;
        private readonly IMapper mapper;
        public NavigationController(INavigationService navigationService, INavigationRelationService navigationRelationService, IMapper mapper,IMemoryCache memoryCache):base(memoryCache)
        {
            this.navigationService = navigationService;
            this.navigationRelationService = navigationRelationService;
            this.mapper = mapper;
        }
        [HttpGet]
        public IActionResult NavigationColumnList()
        {
            var navigationColumns = this.navigationService.GetAllNavigationColumn().OrderBy(x => x.Priority);
            return View(navigationColumns);
        }
        [HttpGet]
        public IActionResult AddNavigationColumn()
        {
            return View();
        }
        [HttpPost]
        public IActionResult AddNavigationColumn(NavigationColumn model)
        {
            if (!ModelState.IsValid)
                return View(model);
            navigationService.AddNavigationColumn(model);
            return RedirectToAction(nameof(NavigationColumnList));
        }
        [HttpGet]
        public IActionResult EditNavigationColumn(int id)
        {
            var model = navigationService.GetNavigationColumnById(id);
            return View(model);
        }
        [HttpPost]
        public IActionResult UpdateNavigationColumn(NavigationColumn model)
        {
            if (!ModelState.IsValid)
                return View(model);
            navigationService.UpdateNavigationColumn(model);
            return RedirectToAction(nameof(NavigationColumnList));
        }
        [HttpPost]
        public IActionResult DeleteNavigationColumn(int[] ids)
        {
            navigationService.DeleteNavigationColumns(ids);
            return Json("ok");
        }

        [HttpGet]
        public IActionResult NavigationInfoList()
        {
            //var models = this.navigationService.GetAllNavigationInfo();
            var models = new List<NavigationInfo>();
            return View(models);
        }
        [HttpGet]
        public IActionResult AddNavigationInfo()
        {
            var vm = new NavigationInfoCreateOrUpdateViewModel();
            vm.AvaliableClasses.AddRange(navigationService.GetAllClasses().Select(x => new SelectListItem { Text = x.Name, Value = x.Id.ToString() }));
            vm.AvaliableParents.AddRange(navigationService.GetAllParentNavigation().Select(x => new SelectListItem { Text = x.Name, Value = x.Id.ToString() }));
            vm.AvaliableNavigationColumns = navigationService.GetAllNavigationColumn().Where(x => x.Id > 1).Select(x => new SelectListItem { Text = x.Name, Value = x.Id.ToString() });
            return View(vm);
        }
        [HttpPost]
        public IActionResult AddNavigationInfo(NavigationInfoCreateOrUpdateViewModel model)
        {
            if (!ModelState.IsValid)
                return View(model);
            var columnIds = model.NavigationColumnIds;
            var navigationInfo = mapper.Map<NavigationInfoEntity>(model);
            if (columnIds == null)
            {
                navigationService.AddNavigationInfo(navigationInfo);
                return RedirectToAction(nameof(NavigationInfoList));
            }
            var relations = columnIds.Select(x => new NavigationRelationEntity { NavigationInfoId = navigationInfo.Id, ClassId = x });
            navigationRelationService.AddNavigationInfoAndRelations(navigationInfo, relations);
            return RedirectToAction(nameof(NavigationInfoList));
        }
        [HttpGet]
        public IActionResult EditNavigationInfo(int id)
        {
            var model = navigationService.GetNavigationById(id);
            var vm = mapper.Map<NavigationInfoCreateOrUpdateViewModel>(model);
            vm.AvaliableClasses.AddRange(navigationService.GetAllClasses().Select(x => new SelectListItem { Text = x.Name, Value = x.Id.ToString() }));
            vm.AvaliableParents.AddRange(navigationService.GetAllParentNavigation().Select(x => new SelectListItem { Text = x.Name, Value = x.Id.ToString() }));
            var relations = navigationRelationService.GetByNavigationId(id).ToList();
            vm.AvaliableNavigationColumns = navigationService.GetAllNavigationColumn().Where(x => x.Id > 1).Select(x => new SelectListItem { Text = x.Name, Value = x.Id.ToString(), Selected = relations.Exists(r => r.ClassId == x.Id) });
            return View(vm);
        }
        [HttpPost]
        public IActionResult UpdateNavigationInfo(NavigationInfoCreateOrUpdateViewModel model)
        {
            if (!ModelState.IsValid)
                return View(model);
            var columnIds = model.NavigationColumnIds;
            var navigationInfo = mapper.Map<NavigationInfoEntity>(model);
            var oldRelations = model.AvaliableNavigationColumns;
            IEnumerable<NavigationRelationEntity> relations = new List<NavigationRelationEntity>();
            if (columnIds != null)
                relations = columnIds.Select(x => new NavigationRelationEntity { NavigationInfoId = navigationInfo.Id, ClassId = x, Priority = 10 });
            navigationRelationService.UpdateNavigationInfoAndRelations(navigationInfo, relations);
            return RedirectToAction(nameof(NavigationInfoList));
        }
        [HttpPost]
        public IActionResult Search(string navigationName, string className, string columnId, bool? isCheck, int page = 1, int limit = 10)
        {
            var navigationCondition = new NavigationCondition
            {
                IsCheck = isCheck,
                NavigationName = navigationName,
                ClassName = className,
                PageIndex = page,
                PageSize = limit,
                TotalCount = 0
            };
            var navigations = navigationService.Search(navigationCondition);
            return Json(new { code = 0, count = navigationCondition.TotalCount, msg = "", data = navigations });
        }
        [HttpPost]
        public IActionResult GetNavigationByColumnId(string columnId, int page = 1, int limit = 10)
        {
            var navigationCondition = new NavigationCondition
            {
                ColumnId = columnId,
                PageIndex = page,
                PageSize = limit,
                TotalCount = 0
            };
            var navigations = navigationService.GetNavigationByColumnId(navigationCondition);
            return Json(new { code = 0, count = navigationCondition.TotalCount, msg = "", data = navigations });
        }
        [HttpPost]
        public IActionResult DeleteNavigationInfo(int[] ids)
        {
            navigationService.DeleteNavigationInfos(ids);
            return Json("ok");
        }
        [HttpGet]
        public IActionResult GetAllNavigationColumn()
        {
            var temp = navigationService.GetAllNavigationColumn().Where(x => x.Id > 1).OrderBy(x => x.Priority);
            var colums = from t in temp
                         select new { t.Id, t.Name };
            return Json(colums);
        }
    }
}